(function(angular) {
  var AppController = function($scope, Trade) {

	  Trade.query(function(response) {
      $scope.trades = response ? response : [];      
    });
	  
    
    
    $scope.addTrade = function(newtrade) {
      new Trade({
    	clientname: newtrade.clientname,
    	buysell: newtrade.buysell,
    	stock: newtrade.stock,
    	volume: newtrade.volume,
    	price: newtrade.price,      
        checked: false
      }).$save(function(trade) {
        $scope.trades.push(trade);
      });
      $scope.newTrade = "";
    };
    
    $scope.updateTrade = function(trade) {
      trade.$update();
    };
    
    $scope.deleteTrade = function(trade) {
      trade.$remove(function() {
        $scope.trades.splice($scope.trades.indexOf(trade), 1);
      });
    };
  };
  
  AppController.$inject = ['$scope', 'Trade'];
  angular.module("myApp.controllers").controller("AppController", AppController);
}(angular));